﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class JobSpecificationDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public JobSpecificationDAL()
        {

        }

        public DataSet LoadListJobSpecifications()
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT JOB_SPEC_SEQ, JOB_SPEC_NAME"
                    + " FROM HRM_MP_JOB_SPECIFICATIONS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord";

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public DataSet LoadGridJobSpecifications()
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT JOB_SPEC_SEQ, JOB_SPEC_NAME, REMARKS"
                    + " FROM HRM_MP_JOB_SPECIFICATIONS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord";

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Insert(string JobSpecName, string Remarks, int CreatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@JobSpecName", JobSpecName);                
                dbManager.AddParameters(1, "@Remarks", Remarks);
                dbManager.AddParameters(2, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(4, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_MP_JOB_SPECIFICATIONS(JOB_SPEC_NAME, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@JobSpecName, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(int JobSpecSeq, string JobSpecName, string Remarks, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@JobSpecSeq", JobSpecSeq);
                dbManager.AddParameters(1, "@JobSpecName", JobSpecName);
                dbManager.AddParameters(2, "@Remarks", Remarks);
                dbManager.AddParameters(3, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(4, "@UpdatedWhen", DateTime.Today);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);

                strSQL = "UPDATE HRM_MP_JOB_SPECIFICATIONS"
                           + " SET JOB_SPEC_NAME = @JobSpecName"
                                + ", REMARKS = @Remarks"
                                + ", RECORD_STATUS = @RecordStatus"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE JOB_SPEC_SEQ = @JobSpecSeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int JobSpecSeq, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", JobSpecSeq);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_MP_JOB_SPECIFICATIONS"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE JOB_SPEC_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
